package com.liruan.rest.codegenerator;

import java.lang.reflect.Field;

public final class DTOBuilderGenerator
{
	public static void generate(Class<?> dtoClass)
	{
		if (dtoClass == null)
		{
			return;
		}

		Field[] fields = dtoClass.getDeclaredFields();
		if (fields.length == 0)
		{
			return;
		}

		String dtoTypeName = dtoClass.getSimpleName();
		StringBuilder builder = new StringBuilder();
		builder.append("public static class Builder{");
		for (Field field : fields)
		{
			String typeName = field.getType().getSimpleName();
			String fieldName = field.getName();
			char first = Character.toUpperCase(fieldName.charAt(0));
			builder.append("private ").append(typeName).append(" ").append(fieldName).append(";");
			builder.append("public Builder set").append(first).append(fieldName.substring(1, fieldName.length()))
					.append("(").append(typeName).append(" ").append(fieldName).append("){this.").append(fieldName)
					.append(" = ").append(fieldName).append(";return this;}");
		}

		builder.append("public ").append(dtoClass.getSimpleName()).append(" build(){")
				.append(dtoTypeName).append(" dto = new ").append(dtoTypeName).append("();");
		for (Field field : fields)
		{
			String fieldName = field.getName();
			builder.append("dto.").append(fieldName)
					.append("=").append("this.").append(fieldName).append(";");

		}

		builder.append("return dto;}");
		builder.append("}");
		System.out.println(builder.toString());
	}

	public static void main(String[] args)
	{
	//	generate(AskDTO.class);
	}
}
